#include "regx51.h"
#include "key.h"




void Delay_ms(unsigned int t);

unsigned char code LED_Table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f
                                //0  1  2  3  4  5  6  7  8  9
                                ,0x77,0x7c,0x39,0x5e,0x79,0x71,0x40,0x00};
                                //A  B  C  D  E  F  -  Down
unsigned char code LED_F_Table[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef
                                //0  1  2  3  4  5  6  7  8  9
                                ,0xf7,0xfc,0xb9,0xde,0xf9,0xf1,0x40,0x00};
                                //A  B  C  D  E  F  -  Down



unsigned char data LED_Seg_DisNum[8]={17,17,17,17,17,17,17,0};
unsigned long int Value=0;
unsigned char key=0;
unsigned char Key_Data=0;
unsigned char Data=0;


void LED_Show(void)
{
	unsigned char scan=0xfe;
    unsigned char i=0;
	for(i=0;i<8;i++)
    {
        P2=0xff;
        P0=LED_Table[LED_Seg_DisNum[i]];
        P2=scan;
        scan=(scan<<1)+1;
        Delay_ms(5);
    }
}

void LED_Clear(void)
{
	unsigned char i=0;
	for(i=0;i<8;i++)
	{
        LED_Seg_DisNum[i]=17;
    }
}

void Show_Seg8_Unsigend_Num(unsigned long int num)
{
	unsigned char Flag=17;
	unsigned char i=0;
    LED_Seg_DisNum[0]=num/10000000;
    num%=10000000;
    LED_Seg_DisNum[1]=num/1000000;
    num%=1000000;
    LED_Seg_DisNum[2]=num/100000;
    num%=100000;
    LED_Seg_DisNum[3]=num/10000;
    num%=10000;
    LED_Seg_DisNum[4]=num/1000;
    num%=1000;
    LED_Seg_DisNum[5]=num/100;
    num%=100;
    LED_Seg_DisNum[6]=num/10;
    num%=10;
    LED_Seg_DisNum[7]=num/1;
	for(i=0;i<7;i++)
	{
		if(LED_Seg_DisNum[i]==0)LED_Seg_DisNum[i]+=Flag;
		else Flag=0;
	}
}

void Show_Seg8_Float(float float_num)
{
    unsigned char float_flag=0;
    unsigned char j=0;
    unsigned long int num=0;
    unsigned char scan=0xfe;
    unsigned char i=0;
    if(float_num<10)
    {
        while(j<7)
        {
            float_num*=10;
            if(float_num<0)
            float_flag+=1;
            j++;
        }
    }
    else
    {
        if(float_num>=10&&float_num<100)
        {
            float_num*=1000000;
            float_flag=1;
        }
        else if (float_num>=100&&float_num<1000)
        {
            float_num*=100000;
            float_flag=2;
        }
        else if (float_num>=1000&&float_num<10000)
        {
            float_num*=10000;
            float_flag=3;
        }
        else if (float_num>=10000&&float_num<100000)
        {
            float_num*=1000;
            float_flag=4;
        }
        else if (float_num>=100000&&float_num<1000000)
        {
            float_num*=100;
            float_flag=5;
        }
        else if (float_num>=1000000&&float_num<10000000)
        {
            float_num*=10;
            float_flag=6;
        }
        else
        {
            float_num*=1;
            float_flag=8;
        }
    }
    num=(unsigned long int)float_num;
    LED_Seg_DisNum[0]=num/10000000;
    num%=10000000;
    LED_Seg_DisNum[1]=num/1000000;
    num%=1000000;
    LED_Seg_DisNum[2]=num/100000;
    num%=100000;
    LED_Seg_DisNum[3]=num/10000;
    num%=10000;
    LED_Seg_DisNum[4]=num/1000;
    num%=1000;
    LED_Seg_DisNum[5]=num/100;
    num%=100;
    LED_Seg_DisNum[6]=num/10;
    num%=10;
    LED_Seg_DisNum[7]=num/1;

    for(i=0;i<8;i++)
    {
        P2=0xff;
        if(i==float_flag)
        P0=LED_F_Table[LED_Seg_DisNum[i]];
        else
        P0=LED_Table[LED_Seg_DisNum[i]];
        P2=scan;
        scan=(scan<<1)+1;
        Delay_ms(5);
    }

}

void Show_Seg8_Bits(unsigned char Data)
{
	unsigned char i=0;
	LED_Clear();
	for(i=0;i<8;i++)
	{
		Data=Data<<1;
		LED_Seg_DisNum[i]=CY;
		CY=0;
	}
}

void Show_Seg8_Hex(unsigned long int Data)
{
	unsigned char Flag=17;
	unsigned char i=0;
	unsigned char j=0;
	unsigned char HEX_Data=0;
	for(i=0;i<8;i++)
	{
		Data=Data<<1;
		if(CY)
		HEX_Data=HEX_Data+8;
		CY=0;
		Data=Data<<1;
		if(CY)
		HEX_Data=HEX_Data+4;
		CY=0;
		Data=Data<<1;
		if(CY)
		HEX_Data=HEX_Data+2;
		CY=0;
		Data=Data<<1;
		if(CY)
		HEX_Data=HEX_Data+1;
		CY=0;
		LED_Seg_DisNum[i]=HEX_Data;
		HEX_Data=0;
	}
	for(i=0;i<3;i++)
	{
		if(LED_Seg_DisNum[2*i]==0&&LED_Seg_DisNum[2*i+1]==0){
		LED_Seg_DisNum[2*i]+=Flag;
		LED_Seg_DisNum[2*i+1]+=Flag;
		}
		else Flag=0;
	}

}

void Timer_Init(void)
{
	TMOD=0x02;
    TL0=0x9c;
    TH0=0x9c;
    ET0=1;
    EA=1;
    TR0=1;
}

void Delay_ms(unsigned int t)
{
    unsigned int i,j;
    for(i=0;i<t;i++)
    {
        for(j=0;j<100;j++)
        {
            ;
        }
    }
}


//
void main()
{
    while(1)
    {
	key=get_key();
		if(key!=0)
		{
			Key_Data=key;
			if(Key_Data==KEY_ONE){if(Value<=9999999)Value=Value*10+1;Show_Seg8_Unsigend_Num(Value);}
			else if(Key_Data==KEY_TWO){if(Value<=9999999)Value=Value*10+2;Show_Seg8_Unsigend_Num(Value);}
			else if(Key_Data==KEY_THREE){if(Value<=9999999)Value=Value*10+3;Show_Seg8_Unsigend_Num(Value);}
			else if(Key_Data==KEY_FOUR){if(Value<=9999999)Value=Value*10+4;Show_Seg8_Unsigend_Num(Value);}
			else if(Key_Data==KEY_FIVE){if(Value<=9999999)Value=Value*10+5;Show_Seg8_Unsigend_Num(Value);}
			else if(Key_Data==KEY_SIX){if(Value<=9999999)Value=Value*10+6;Show_Seg8_Unsigend_Num(Value);}
			else if(Key_Data==KEY_SEVEN){if(Value<=9999999)Value=Value*10+7;Show_Seg8_Unsigend_Num(Value);}
			else if(Key_Data==KEY_EIGHT){if(Value<=9999999)Value=Value*10+8;Show_Seg8_Unsigend_Num(Value);}
			else if(Key_Data==KEY_NINE){if(Value<=9999999)Value=Value*10+9;Show_Seg8_Unsigend_Num(Value);}
			else if(Key_Data==KEY_ZERO){if(Value<=9999999)Value=Value*10+0;Show_Seg8_Unsigend_Num(Value);}
			else if(Key_Data==KEY_ADD){;}
			else if(Key_Data==KEY_SUB){Show_Seg8_Hex(Value);}
			else if(Key_Data==KEY_MUL){Show_Seg8_Bits(Value);}
			else if(Key_Data==KEY_DIV){Show_Seg8_Unsigend_Num(Value);}
			else if(Key_Data==KEY_EQU){;}
			else if(Key_Data==KEY_ON){Value=0;Show_Seg8_Unsigend_Num(Value);}
		}
        LED_Show();
    }
}

